//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.pc_rfid.api;

import java.util.Arrays;

public abstract class PC_COMM {
    private static final int ParseDatalen = 1024;
    private static final String TAG = "PC_COMM";
    protected boolean isOpen = false;
    protected byte[] ParseData = new byte[1024];
    protected int ParseBegin = 0;
    protected int ParseLen = 0;

    public PC_COMM() {
    }

    public abstract int Send(byte[] var1);

    public abstract int Receive(byte[] var1);

    public abstract int close();

    public abstract void clearBuffer();

    public abstract int Send_Receive(RFdata var1);

    public boolean ParsePack(byte[] ReceiveData, int len, byte[] pack) {
        if (this.ParseLen == 0 && len <= 0) {
            return false;
        } else {
            int ParseEnd = this.ParseBegin + this.ParseLen;
            if (ParseEnd < 0) {
                Arrays.fill(this.ParseData, (byte)0);
                this.ParseBegin = 0;
                this.ParseLen = 0;
                ParseEnd = 0;
            }

            int i;
            if (len > 0) {
                this.ParseLen += len;

                for(i = 0; i < len; ++ParseEnd) {
                    ParseEnd %= 1024;
                    this.ParseData[ParseEnd] = ReceiveData[i];
                    ++i;
                }
            }

            i = this.ParseBegin + this.ParseLen;

            for(int k = this.ParseBegin; k < i; ++k) {
                int pos = k % 1024;
                if (this.ParseData[pos] == -86) {
                    int packLen = (this.ParseData[(pos + 1) % 1024] & 255) + 1;
                    if (packLen >= 6 && packLen <= this.ParseLen - pos + this.ParseBegin) {
                        int pos1 = pos;

                        for(int j = 0; j < packLen; ++pos1) {
                            pos1 %= 1024;
                            pack[j] = this.ParseData[pos1];
                            ++j;
                        }

                        int crc = crc16(pack);
                        if (pack[packLen - 1] == (byte)(crc / 256) && pack[packLen - 2] == (byte)(crc % 256)) {
                            this.ParseLen -= packLen;

                            for(int j = 0; j < packLen; ++this.ParseBegin) {
                                this.ParseBegin %= 1024;
                                this.ParseData[this.ParseBegin] = 0;
                                ++j;
                            }

                            return true;
                        }
                    }
                }
            }

            return false;
        }
    }

    public static char crc16(byte[] data, int beginPos) {
        ++beginPos;
        int len = data[beginPos];
        if (len < 0) {
            len += 256;
        }

        len -= 2;
        int CRC = 61166;
        if (len == 0) {
            return '\u0000';
        } else {
            for(int i = 0; i < len; ++i) {
                CRC ^= data[beginPos + i] & 255;

                for(int j = 0; j < 8; ++j) {
                    if ((CRC & 1) != 0) {
                        CRC = CRC >> 1 ^ '萈';
                    } else {
                        CRC >>= 1;
                    }
                }
            }

            return (char)CRC;
        }
    }

    public static char crc16(byte[] data) {
        return crc16(data, 0);
    }

    protected void clearParse() {
        Arrays.fill(this.ParseData, (byte)0);
        this.ParseBegin = 0;
        this.ParseLen = 0;
    }
}
